import 'dart:async';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_soundmagus/page/device/page_device.dart';
import 'package:flutter_soundmagus/widget/alert/alert.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:get/get.dart';

import '../save/page_save.dart';

enum Menu {
  close,
  home,
  save,
  upload,
  setting,
  summing,
  cross,
  delay,
  gain,
  eq,
}

class MenuController extends GetxController {
  final index = 0.obs;
  static const pages = [Menu.home, Menu.summing, Menu.cross, Menu.delay, Menu.gain, Menu.eq];
  void onMenu(Menu menu) {
    var i = pages.indexOf(menu);
    if (i != -1) {
      //eq home等页面
      index.value = i;
      Get.back();
      return;
    }
    if (menu == Menu.close) {
      Get.back();
    } else if (menu == Menu.save) {
      Get.dialog(PageSave());
    } else if (menu == Menu.upload) {
      showUpload();
    } else if (menu == Menu.setting) {
      Get.dialog(PageDevice());
    }
  }

  showUpload() {
    // if (!BleHelper().connected) {
    if (FlutterBlue.connectedDevices.length <= 0) {
      EasyLoading.showToast('Bluetooth not connect.');
      return;
    }
    Get.dialog(AlertUploadConfirm(
      onPressed: () {
        showLoading(duration: Duration(seconds: 4));
        MsgParser.instance.start();
      },
    ));
  }

  StreamSubscription? _onFinishedSubscription;
  void onFinished(bool needSendData) {
    Get.back();
    _onFinishedSubscription?.cancel();
    _onFinishedSubscription = null;
  }

  @override
  void onInit() {
    super.onInit();
  }
}
